﻿using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace txt_file_reader
{
    internal class Line
    {
        public string text = "";
        public byte[] voice = null;
        private int lineIndex = -1;
        public string mp3Name = "";
        private bool mp3Loaded = false;
        private volatile bool cancel = false;
        public void Cancel()
        {
            cancel = true;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]

        internal void asynLoadVoice()
        {
            Task.Run(() => {
                if (!File.Exists(mp3Name))
                {
                    MicrosoftTts.ConvertTxt2Voice(this.text, this.mp3Name);
                }
                this.mp3Loaded = true;
            });
            
        }

        internal void destory()
        {
            if(this.mp3Name != null && this.mp3Name.Length > 0 && this.mp3Loaded)
            {
                String filePath = this.mp3Name;
                this.mp3Loaded = false;
                File.Delete(filePath);
            }
        }

        internal void setText(string text,int index)
        {
            if (string.IsNullOrEmpty(text) || text == "\0")
            {
                this.text = "";
                this.mp3Name = "";
                return;
            }
            this.text = text;
            this.lineIndex = index;
            this.mp3Name = string.Format("./tmp-voice-{0}.mp3", index);
            this.asynLoadVoice();
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public string getMp3Name(long maxSeconds = 20)
        {
            DateTime current = DateTime.Now;
            while (current.AddMilliseconds(maxSeconds*1000) > DateTime.Now)
            {
                if (cancel)
                {
                    return "";
                }
                if (this.mp3Loaded)
                {
                    return this.mp3Name;
                }
                Application.DoEvents();
            }
            return "";

        }
        internal void playThenDestory()
        {
            this.cancel = false;
            MP3Player player = new MP3Player();
            if (!this.mp3Loaded)
            {
                this.asynLoadVoice();
            }
            player.FileName = this.getMp3Name();
            player.play();
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            //MicrosoftTts.ConvertTxt2Voice(this.text, this.mp3Name);
            //sw.Stop();
            //long taken = sw.ElapsedMilliseconds;
            //string tmp = _path;
            //_path = _next_path;
            //_next_path = tmp;
            bool noCancel = delay(player.Duration);
            player.StopT();
            //如果不是正常播放完毕则是暂停,那么不删除文件
            if (noCancel) this.destory();
        }
        public bool delay(int mm)
        {
            DateTime current = DateTime.Now;
            while (current.AddMilliseconds(mm) > DateTime.Now)
            {
                if (cancel)
                {
                    return false;
                }
                Application.DoEvents();
            }
            return true;
        }
        internal void stop()
        {
            this.cancel = true;
        }
    }
}